/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.resource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cleangwt.json.client.SimpleObject;
import com.cleangwt.showcase.client.resource.i18n.ShowcaseMessages;
import com.cleangwt.user.shared.Link;
import com.cleangwt.user.shared.Option;

/**
 * The data source provider.
 * 
 * @author Jess
 * @since 2011/10/26
 */
public class DataSource {

  private static ShowcaseMessages c = ShowcaseMessages.Impl;

  private static List<Option> interestingItems;

  private static List<Option> interestingItems2;

  private static List<Option> maleItems;

  private static Map<String, List<Option>> OPTIONS = new HashMap<String, List<Option>>();

  public static List<Option> getABCOptions() {
    return Arrays.asList(new Option("A", "a"), new Option("B", "b"), new Option("C", "c"),
        new Option("D", "d"), new Option("E", "e"));
  }

  public static List<SimpleObject> getDepartments() {
    List<SimpleObject> departments = new ArrayList<SimpleObject>();
    for (int i = 0; i < 3; i++) {
      SimpleObject dept = new SimpleObject();
      dept.put("deptId", i);
      dept.put("name", c.departmentN("" + i));
      dept.put("units", getUnits(i));
      departments.add(dept);
    }
    return departments;
  }

  public static List<Option> getInterestings() {
    if (interestingItems == null) {
      return Arrays.asList(new Option(c.sport(), "A"), new Option(c.reading(), "B"), new Option(c
          .social(), "C"), new Option(c.dance(), "D"), new Option(c.singing(), "E"));
    }
    return interestingItems;
  }

  public static List<Option> getInterestings2() {
    if (interestingItems2 == null) {
      return Arrays.asList(new Option(c.select(), ""), new Option(c.sport(), "A"), new Option(c
          .reading(), "B"), new Option(c.social(), "C"), new Option(c.dance(), "D"), new Option(c
          .singing(), "E"));
    }
    return interestingItems2;
  }

  public static List<Link> getLayeredLinks2() {
    Link link11 = new Link(c.widget());
    Link link12 = new Link(c.validator());
    Link link13 = new Link(c.compond());
    Link link14 = new Link(c.tutorial());

    // level3
    link11.addSubLink(new Link(c.button(), "ButtonDemo"));
    link11.addSubLink(new Link(c.formControl(), "FormControlDemo"));
    link11.addSubLink(new Link(c.navigator(), "NavigatorDemo"));
    link11.addSubLink(new Link(c.layout(), "LayoutDemo"));
    link11.addSubLink(new Link(c.popup(), "PopupDemo"));

    link11.addSubLink(new Link(c.carousel(), "CarouselDemo"));
    link11.addSubLink(new Link(c.datatable(), "DataGridDemo"));
    link11.addSubLink(new Link(c.datatree(), "CellTreeDemo"));

    link12.addSubLink(new Link(c.basevalidation(), "BaseValidationDemo"));
    link12.addSubLink(new Link(c.groupvalidation(), "GroupValidationDemo"));
    link12.addSubLink(new Link(c.dynamicvalidation(), "DynamicValidationDemo"));
    link12.addSubLink(new Link(c.remotevalidation(), "RemoteValidationDemo"));

    link13.addSubLink(new Link(c.usermanager(), "UserManage"));
    link13.addSubLink(new Link(c.shoppingcart(), "ShoppingCart"));

    link14.addSubLink(new Link(c.pageContext(), "PageContextTutorial"));
    link14.addSubLink(new Link(c.activityManager(), "ActivityManagerTutorial"));
    link14.addSubLink(new Link(c.uibinder(), "UiBinderTutorial"));
    link14.addSubLink(new Link(c.simpleObject(), "SimpleObjectTutorial"));
    link14.addSubLink(new Link(c.gettingStarted(), "GettingStarted"));
    return Arrays.asList(link11, link12, link13, link14);
  }

  public static List<Link> getLayeredLinks3() {
    Link link11 = new Link(c.widget());

    Link link12 = new Link(c.validator());
    Link link13 = new Link(c.compond());
    Link link14 = new Link(c.tutorial());

    Link link21 = new Link(c.basewidget());
    Link link22 = new Link(c.richwidget());
    Link link24 = new Link(c.validator());
    Link link25 = new Link(c.datamanager());
    Link link26 = new Link(c.flowcontrol());
    Link link27 = new Link(c.tutorial());

    // level2
    link11.addSubLink(link21);
    link11.addSubLink(link22);

    link12.addSubLink(link24);

    link13.addSubLink(link25);
    link13.addSubLink(link26);
    link14.addSubLink(link27);

    // link15.addSubLink(link27);

    // level3
    link21.addSubLink(new Link(c.button(), "ButtonDemo"));
    link21.addSubLink(new Link(c.formControl(), "FormControlDemo"));
    link21.addSubLink(new Link(c.navigator(), "NavigatorDemo"));
    link21.addSubLink(new Link(c.layout(), "LayoutDemo"));
    link21.addSubLink(new Link(c.popup(), "PopupDemo"));

    link22.addSubLink(new Link(c.carousel(), "CarouselDemo"));
    link22.addSubLink(new Link(c.datatable(), "DataGridDemo"));
    link22.addSubLink(new Link(c.datatree(), "CellTreeDemo"));

    link24.addSubLink(new Link(c.basevalidation(), "BaseValidationDemo"));
    link24.addSubLink(new Link(c.groupvalidation(), "GroupValidationDemo"));
    link24.addSubLink(new Link(c.dynamicvalidation(), "DynamicValidationDemo"));
    link24.addSubLink(new Link(c.remotevalidation(), "RemoteValidationDemo"));

    link25.addSubLink(new Link(c.usermanager(), "UserManage"));
    link26.addSubLink(new Link(c.shoppingcart(), "ShoppingCart"));

    link27.addSubLink(new Link(c.pageContext(), "PageContextTutorial"));
    link27.addSubLink(new Link(c.activityManager(), "ActivityManagerTutorial"));
    link27.addSubLink(new Link(c.uibinder(), "UiBinderTutorial"));
    link27.addSubLink(new Link(c.simpleObject(), "SimpleObjectTutorial"));
    link27.addSubLink(new Link(c.gettingStarted(), "GettingStarted"));
    return Arrays.asList(link11, link12, link13, link14);
  }

  public static List<SimpleObject> getProducts() {
    List<SimpleObject> products = new ArrayList<SimpleObject>();
    for (int i = 0; i < 18; i++) {
      SimpleObject product = new SimpleObject();
      product.put("id", i);
      product.put("name", c.product() + i);
      product.put("img", "" + i);
      product.put("desc", c.prodcutDesc("" + i));
      product.put("detailDesc", c.detailDesc());
      product.put("price", 1000);
      products.add(product);
    }
    return products;
  }

  public static List<Option> getRecordOptions() {
    if (!OPTIONS.containsKey("record")) {
      OPTIONS.put("record", Arrays.asList(new Option(c.reset(), "reset"), new Option(c.cancel(),
          "cancel"), new Option(c.delete(), "delete"), new Option(c.save(), "save")));
    }
    return OPTIONS.get("record");
  }

  public static List<Option> getSexTypes() {
    if (maleItems == null) {
      maleItems = Arrays.asList(new Option(c.female(), "F"), new Option(c.male(), "M"));
    }
    return maleItems;
  }

  public static List<SimpleObject> getUnits(int dept) {
    List<SimpleObject> units = new ArrayList<SimpleObject>();
    for (int i = 0; i < 3; i++) {
      int index = i + dept * 3;
      SimpleObject unit = new SimpleObject();
      unit.put("unitId", index);
      unit.put("name", c.unitN("" + index));
      unit.put("users", getUsers(index));
      units.add(unit);
    }
    return units;
  }

  public static List<SimpleObject> getUsers(int unit) {
    List<SimpleObject> users = new ArrayList<SimpleObject>();
    for (int i = 0; i < 20; i++) {
      int index = i + unit * 20;
      SimpleObject user = new SimpleObject();
      user.put("userId", index);
      user.put("name", c.userN("" + index));
      user.put("age", i);
      user.put("address", c.addressN("" + i));
      users.add(user);
    }
    return users;
  }

}